package com.zhixiang.core.web.servlet;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fr.base.FRContext;
import com.fr.log.FRLogManager;
import com.fr.report.script.Calculator;
import com.fr.web.Browser;
import com.fr.web.J2EEContainer;
import com.fr.web.core.ErrorHandlerHelper;
import com.fr.web.core.ReportDispatcher;
import com.fr.web.core.ServerEnv;
import com.fr.web.core.gzip.GZIPResponseWrapper;
import com.fr.web.platform.exception.RedirectException;

public class ReportServlet extends HttpServlet {
    private static final long serialVersionUID = 0x1e99035cd25ee27bL;
    public static String APP_NAME = null;

    public ReportServlet() {
    }

    public void init(ServletConfig servletconfig) throws ServletException {
        super.init(servletconfig);
        J2EEContainer.init(servletconfig);
        ServletContext servletcontext = getServletContext();
        try {
            if (FRContext.getCurrentEnv() == null)
                FRContext.setCurrentEnv(new ServerEnv(servletcontext));
            FRContext.getLogger().initRecord();
        } catch (Exception exception) {
            FRContext.getLogger().error(exception.getMessage());
        }
        servletcontext.setAttribute(getClass().getName(), this);
    }
/*
    public void destroy(ServletConfig servletconfig) {
        FRContext.getLogger().closeRecord();
        FRContext.getLogger().close();
    }
*/
    public void doGet(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
            throws IOException, ServletException {
        if (APP_NAME == null) {
            APP_NAME = httpservletrequest.getContextPath();
            if (APP_NAME.startsWith("/"))
                APP_NAME = APP_NAME.substring(1);
        }
        Calculator.saveRequestContext(httpservletrequest);
        GZIPResponseWrapper gzipresponsewrapper = null;
        try {
            String s = httpservletrequest.getHeader("accept-encoding");
            if (FRContext.getConfigManager().isSupportGzip() && !"false".equals(httpservletrequest.getParameter("gzip"))
                    && s != null && s.indexOf("gzip") != -1 && Browser.resolve(httpservletrequest).supportGzip())
                gzipresponsewrapper = new GZIPResponseWrapper(httpservletresponse);
            if (gzipresponsewrapper != null)
                httpservletresponse = gzipresponsewrapper;
            httpservletresponse.addHeader("P3P", "CP=CAO PSA OUR");
            ReportDispatcher.dealWithRequest(httpservletrequest, httpservletresponse);
        } catch (RedirectException localRedirectException) {
            FRContext.getLogger().error(localRedirectException.getMessage());
            /*
             * }catch (RegistEditionException localRegistEditionException) {
             * zyfang 可能与授权有关if(localRegistEditionException.isAjax()) {
             * PrintWriter printwriter =
             * WebUtils.createPrintWriter(httpservletresponse); JSONObject
             * jsonobject = new JSONObject(); try { jsonobject.put("exception",
             * "failpass"); if(localRegistEditionException.getFUNC() != null)
             * jsonobject.put("func",
             * localRegistEditionException.getFUNC().toString()); }
             * catch(JSONException jsonexception) {
             * jsonexception.printStackTrace(); }
             * printwriter.write(jsonobject.toString()); printwriter.flush();
             * printwriter.close(); return; }
             * FRContext.getLogger().errorWithServerLevel(
             * localRegistEditionException.getMessage(),
             * localRegistEditionException);
             * ErrorHandlerHelper.getErrorHandler().error(httpservletrequest,
             * httpservletresponse, localRegistEditionException);
             */
        } catch (Exception localException) {
            FRContext.getLogger().errorWithServerLevel(localException.getMessage(), localException);
            ErrorHandlerHelper.getErrorHandler().error(httpservletrequest, httpservletresponse, localException);
        } catch (OutOfMemoryError localOutOfMemoryError) {
            FRContext.getLogger().errorWithServerLevel(localOutOfMemoryError.getMessage(), localOutOfMemoryError);
            ErrorHandlerHelper.getErrorHandler().error(httpservletrequest, httpservletresponse, localOutOfMemoryError);
            System.gc();
        } finally {
            if (gzipresponsewrapper != null)
                gzipresponsewrapper.finishResponse();
            Calculator.clearThreadSaved();
            FRLogManager.clearThreadSaved();
        }
    }

    public void doPost(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
            throws ServletException, IOException {
        doGet(httpservletrequest, httpservletresponse);
    }

}

